package cn.bone.motan.service;

import cn.bone.api.FooService;
import cn.bone.motan.config.SpringFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;

import javax.annotation.Resource;

/**
 * Created by MD-01 on 2017/3/8.
 */
public class FooServiceImpl implements FooService {

    private Logger logger = LoggerFactory.getLogger("远程服务b");

   /* @Resource(name="remoteService")
    private FooService fooService;

    @Resource(name="remoteService2")
    private FooService fooService2;

    @Override
    public String hello(String name) {

        logger.info("hello()被调用");
        String result = null;
        if(StringUtils.isNotEmpty(name)&&name.contains("b")){
            logger.info("参数中包含b,自己处理");
            result = "b -> hello: " + name;
        }else{

            if(StringUtils.isNotEmpty(name)&&name.contains("2")){
                logger.info("参数中包含2,转发给a2服务");
                result = fooService2.hello(name);
            }else {
                logger.info("参数中不包含2,转发给a服务");
                result = fooService.hello(name);
            }
        }
        return result;
    }*/

    /**
     * 新的实现方式,根据参数的不同从SpringFactory中获取不同的bean
     */
    private FooService fooService;

    @Override
    public String hello(String name){

        String result = null;
        if(StringUtils.isEmpty(name)){
            return "参数name不能为空";
        }
        if(name.contains("b")){
            logger.info("参数中包含b,自己处理");
            result = "b -> hello: " + name;
        }else if(name.contains("2")){
            logger.info("参数中包含2,转发给a2服务");
            fooService = SpringFactory.getBean("remoteService2",FooService.class);
            result = fooService.hello(name);
        }else{
            logger.info("参数中不包含2,转发给a服务");
            fooService = SpringFactory.getBean("remoteService",FooService.class);
            result = fooService.hello(name);
        }
        return result;
    }
}
